package com.itheima.streamdemo1;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class demo3 {

    public static void main(String[] args) {

        //Stream流的终结方法：调用完成后，不会返回新Stream了，无法继续使用流了。

        List<Dog> dogs = new ArrayList<>();
        dogs.add(new Dog("小花",5,"花色","母",7.5));
        dogs.add(new Dog("小白",4,"白色","公",6.5));
        dogs.add(new Dog("小黑",6,"黑色","公",8.5));
        dogs.add(new Dog("小黄",7,"黄色","母",7.0));
        dogs.add(new Dog("小灰",3,"灰色","公",5.5));

        //筛选年龄大于5的
        dogs.stream().filter(dog -> dog.getAge() > 5).forEach(System.out::println);

        System.out.println("------------------------------------");

        //筛选年龄大于5的，统计个数
        long count = dogs.stream().filter(dog -> dog.getAge() > 5).count();
        System.out.println(count);

        System.out.println("------------------------------------");

        //获取体重最大的
        Optional<Dog> max = dogs.stream().max((d1, d2) -> Double.compare(d1.getWeight(), d2.getWeight()));
        Dog maxDog = max.get();     //获取Optional对象中的Dog对象
        System.out.println(maxDog);
        //获取体重最小的
        Optional<Dog> min = dogs.stream().min((d1, d2) -> Double.compare(d1.getWeight(), d2.getWeight()));
        Dog minDog = min.get();
        System.out.println(minDog);

        System.out.println("------------------------------------");

        //Stream流的收集：将流中的数据收集到集合或数组中去
        //流只能收集一次
        List<String> list = new ArrayList<>();
        list.add("张三丰");
        list.add("田七");
        list.add("张无忌");
        list.add("张无忌");
        list.add("李四");
        list.add("张三");
        list.add("张三");
        list.add("张翠山");
        System.out.println(list);

        Stream<String> stream1 = list.stream().filter(s -> s.startsWith("张") && s.length() == 3);

        //收集到List集合中去
        List<String> list1 = stream1.collect(Collectors.toList());
        System.out.println(list1);

        System.out.println("------------------------------------");

        //收集到Set集合中去
        /*Set<String> set = stream1.collect(Collectors.toSet());
        System.out.println(set);        //报错*/
        Stream<String> stream2 = list.stream().filter(s -> s.startsWith("张") && s.length() == 3);
        Set<String> set = stream2.collect(Collectors.toSet());
        System.out.println(set);

        Set<String> set1 = new HashSet<>();
        set1.addAll(list1);
        System.out.println(set1);

        System.out.println("------------------------------------");

        //收集到数组中去
        Stream<String> stream3 = list.stream().filter(s -> s.startsWith("张") && s.length() == 3);
        Object[] array = stream3.toArray();
        System.out.println("数组：" + Arrays.toString(array));

        System.out.println("------------------------------------");

        //收集到Map集合，key：姓名，value：年龄
        /*Map<String,Integer> map = dogs.stream().collect(Collectors.toMap(new Function<Dog, String>() {
            @Override
            public String apply(Dog dog) {
                return dog.getName();
            }
        }, new Function<Dog, Integer>() {
            @Override
            public Integer apply(Dog dog) {
                return dog.getAge();
            }
        }));*/
        //Map<String,Integer> map = dogs.stream().collect(Collectors.toMap(d->d.getName(), d->d.getAge()));
        Map<String,Integer> map = dogs.stream().collect(Collectors.toMap(Dog::getName, Dog::getAge));
        System.out.println(map);

    }
}
